/* CUC Online Judge - A cross-platform judge online system
 * Base on [Verwandlung Online Judge](https://github.com/hzxie/voj)
 * Copyright (C) 2021 Jinhe Zhang <307100890@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *                             /\/`\/\
 *                            _\ | | /_
 *                          -           -
 *                        /               \
 *                       | |-------------| |
 *                       | |^=-_#   #_-=^| |
 *                       \/ \O__/,_ \O__/ \/
 *                        \  \\\     \\\  /
 *                         _\     o     /_
 *                        |/ `--=___=--` \|
 *                       /              六 \
 *                      |  ____       __七_ |
 *                       \____/_\   /_\____/
 */
package org.verwandlung.voj.web.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.verwandlung.voj.web.mapper.ClassCMapper;
import org.verwandlung.voj.web.mapper.ContestMapper;
import org.verwandlung.voj.web.mapper.LanguageMapper;
import org.verwandlung.voj.web.mapper.StudentMapper;
import org.verwandlung.voj.web.model.Language;
import org.verwandlung.voj.web.model.Student;
import org.verwandlung.voj.web.model.User;
import org.verwandlung.voj.web.model.ClassC;
import org.verwandlung.voj.web.model.Contest;

/**
 * 用户类(User)的业务逻辑层.
 * 
 * @author Haozhe Xie
 */
@Service
@Transactional
public class ClassCService {
	/**
	 * 通过用户唯一标识符获取用户对象.
	 * @param userId - 用户唯一标识符
	 * @return 预期的用户对象或空引用
	 */
	public List<ClassC> getAllClasses(String keyword1, String keyword2, Language lang, User teacher, long offset, int limit) {
		List<ClassC> classCs = classCMapper.getAllClasses(keyword1, keyword2, lang, teacher, offset, limit);
		return classCs;
	}

	public long getCountofAllClasses(String keyword, User teacher) {
		long classCnt = classCMapper.getCountofAllClasses(keyword, teacher);
		return classCnt;
	}

	public ClassC getClassof(Long cid) {
		return classCMapper.getClassof(cid);
	}

	public List<Contest> getContestofClassCid(long cid) {
		return contestMapper.getContestofClassCid(cid);
	}

	public Map<String, Boolean> createClass(String classname, String lessonname, String lessonId, String lessonNum,
	String teacher, List<Map<String,String> > students, String languageSlug, String term, User user) {
		int studentcount = students.size();
		Map<String, Boolean>  result = new HashMap<String, Boolean>(3,1);
		Language language = languageMapper.getLanguageUsingSlug(languageSlug);
		ClassC classC = new ClassC(classname, lessonname, lessonId, lessonNum, teacher, studentcount, language, term, user);
		Boolean flag = classCMapper.createClass(classC)!=0?true:false;
		result.put("isSuccessful", flag);
		if ( result.get("isSuccessful") ) {
			for(Map<String,String> student: students){
				Student stu = new Student(student.get("name"),student.get("code"), classC);
				studentMapper.createStudent(stu);
			}
		}
		return result;
	}
	
	public Map<String, Boolean> deleteClass(Long cid) {
		Map<String, Boolean>  result = new HashMap<String, Boolean>(3,1);
		List<Student> students = studentMapper.getStudentsof(cid, 0, INF);
		for(Student student: students){
			studentMapper.deleteStudent(student.getStuid());
		}
		Boolean flag = classCMapper.deleteClass(cid)!= 0?true:false;
		result.put("isSuccessful", flag);
		return result;
	}
	
	@Autowired
	private ClassCMapper classCMapper;
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private LanguageMapper languageMapper;
	@Autowired
	private ContestMapper contestMapper;
	
	private static final int INF = Integer.MAX_VALUE;
}
